import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;


public class NaverCafeAPI {
	
    static final String REQUEST_TOKEN_URL = "https://nid.naver.com/naver.oauth?mode=req_req_token";
    static final String AUTHORIZE_URL = "https://nid.naver.com/naver.oauth?mode=auth_req_token";
    static final String ACCESS_TOKEN_URL = "https://nid.naver.com/naver.oauth?mode=req_acc_token";
    
    static final String CONSUMER_KEY = "6eb_KtTH6Jjx";
    static final String CONSUMER_SECRET = "4E928A58A2iAueBz4Mdx";
    
    static final String CAFE_MENU_LIST_URL = "http://openapi.naver.com/cafe/getMenuList.xml";
    static final String CAFE_ARTICLE_LIST_URL = "http://openapi.naver.com/cafe/getArticleList.xml";

    static final String ACCESS_TOKEN = "nDXmtFdy3qMWM3jayeLz4ZI5ascqpt";
    static final String TOKEN_SECRET = "Tjr60XojMsrJlR2KxPIeDPRl4pyQRZ";
    
    // Service Provider 객체 생성
    static OAuthProvider provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL);   
    // Consumer 객체 생성
    static OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
    
    public static void main(String[] args) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException, IOException, URISyntaxException {         

            consumer.setTokenWithSecret(ACCESS_TOKEN, TOKEN_SECRET);
            
            String clubid = "17281865";
            String searchPage = "1";
            String searchPerPage = "3";
//            URL url = new URL(CAFE_MENU_LIST_URL + "?clubid=" + clubid + "&search.page=" + searchPage + "&search.perPage=" + searchPerPage);               
            
//            http://m.cafe.naver.com/ArticleRead.nhn?clubid=17281865&articleid=11537&page=1&boardtype=L&menuid=1
            String menuid = "3";
            URL url = new URL(CAFE_ARTICLE_LIST_URL + "?search.clubid=" + clubid + "&search.menuid=" + menuid + "&search.page=" + searchPage + "&search.perPage=" + searchPerPage);               

            HttpURLConnection request = (HttpURLConnection) url.openConnection();
            
//            http://club.cyworld.com/club/board/general/ListNormal.asp?club_id=52162178&board_no=8&list_type=&cpage=&search_block=1&Scpage=&search_type=&search_keyword=&board_type=1&show_type=1&headtag_seq=#
            	
            http://club.cyworld.com/club/board/general/list.asp?club_id=52162178&amp;board_no=8&amp;board_type=1            
//"http://mcafethumb.phinf.naver.net/20111001_213/phu7061_1317396597441UY2O0_jpg/p1020152_phu7061.jpg
            http://m.cafe.naver.com/ArticleRead.nhn?clubid=17281865&articleid=11485&page=1&boardtype=L&menuid=3
            // oauth_signature 값을 얻습니다.
            consumer.sign(request);         
            
            request.connect();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
            String tmpStr = "";
            while( (tmpStr = br.readLine()) != null) {
                    System.out.println(tmpStr);
            }
                            
    }
}
